// pages/login/login.js
Page({
  /**
   * 页面的初始数据
   */
  data: {

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)

    getApp().globalData.openid = wx.getStorageSync('openid');
    var userInfo = wx.getStorageSync('userInfo');
    getApp().globalData.userInfo = userInfo;
    this.options = options;

    // 判断是否已登录
    if (userInfo.userId == undefined || userInfo.userId.length < 1 || getApp().globalData.openid == undefined || getApp().globalData.openid == null) {
      this.relogin(); // 重新登录
    } else {
      // 进入首页
      this.openHome(options);
    }
  },

  openHome: function (options) {
    if (options.shareUserId != null && options.shareUserId.length > 0) {
      wx.reLaunch({
        url: '../home/index?shareUserId=' + options.shareUserId + '&acid=' + options.acid + '&tid=' + options.tid + '&nickname=' + getApp().globalData.userInfo.nickName
      })
    } else if (options.detail==1) { // 进入详情页 
      wx.reLaunch({
        url: '../home/index?detail=1&tid=' + options.tid + '&acid=' + options.acid
      })
    } else if (options.supportme == 1) { // 支持我的
      wx.reLaunch({
        url: '../home/index?supportme=1&tid=' + options.tid + '&acid=' + options.acid
      })
    } else if (options.me==1) { // 我的
      wx.reLaunch({
        url: '../home/index?me=1'
      })
    }  else { // 正常进入
      wx.reLaunch({
        url: '../home/index',
      })
    }
  },

  /// 重新登录
  relogin: function () {
    var that = this;
    wx.showLoading();
    wx.login({
      success(res) {
        console.log(res)
        that.code2Session(res.code, options)
      },
      complete () {
        wx.hideLoading();
      }
    })
  },

  code2Session: function(jsCode, options) {
    var that = this;
    console.log(jsCode)

    wx.showLoading();
    wx.request({
      url: getApp().globalData.code_session_url,
      data: {
        jsCode: jsCode,
      },
      success(res) {
        console.log(res)
        getApp().globalData.openid = res.data.data.openid;

      },
      complete () {
        wx.hideLoading();
      }

    })

  },


  onGotUserInfo: function (e) {
    var that = this;


    var that = this;
    wx.showLoading();
    wx.login({
      success(res) {
        console.log(res)
        var jsCode = res.code;
        wx.showLoading();
        wx.request({
          url: getApp().globalData.code_session_url,
          data: {
            jsCode: jsCode,
          },
          success(res) {
            console.log(res)
            getApp().globalData.openid = res.data.data.openid;

            // -------
            if (e.detail.errMsg.indexOf("ok") != -1) { // 成功

              var userIcon = e.detail.userInfo.avatarUrl;
              var nickName = e.detail.userInfo.nickName;

              wx.request({
                url: getApp().globalData.user_add_url,
                data: {
                  openId: getApp().globalData.openid,
                  unionid: getApp().globalData.openid,
                  userIcon: userIcon,
                  nickName: nickName
                },
                success(res) {
                  console.log(res);
                  if (res.data.code == 0) {
                    // 保存用户信息
                    wx.setStorageSync('userInfo', res.data.data);
                    wx.setStorageSync('openid', getApp().globalData.openid)
                    getApp().globalData.userInfo = res.data.data;

                    that.openHome(that.options);
                  }
                }
              })
            }
            // -------



          },
          complete() {
            wx.hideLoading();
          }

        })





      },
      complete() {
        wx.hideLoading();
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {




  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(e) {
    if (e.from == 'menu') { // 分享小程序

      return {
        title: '快来加入闲置会发光，做最环保的时髦精>>',
        path: '/pages/login/login',
        imageUrl: '/resources/share_miniprogram.png',
      }
    }
  }
})